package com.simple.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simple.common.PageResult;
import com.simple.dto.BankDTO;
import com.simple.dto.IncomeDTO;
import com.simple.mapper.IncomeMapper;
import com.simple.pojo.Income;
import com.simple.service.IncomeService;
import com.simple.utils.BeanConverterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName IncomeServiceImpl
 * @Author SimpleTree
 **/
@Service
public class IncomeServiceImpl implements IncomeService {

    @Resource
    private IncomeMapper incomeMapper;
    @Autowired
    private BeanConverterUtil beanConverterUtil;

    @Override
    public PageResult getIncomes(IncomeDTO incomeDTO) {
        PageHelper.startPage(incomeDTO.getPageNum(), incomeDTO.getPageSize(),"create_time desc");

        Example example = new Example(Income.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", incomeDTO.getUserId());
        List<Income> incomes = incomeMapper.selectByExample(example);

        PageInfo<Income> pageInfo = new PageInfo<>(incomes);
        return new PageResult(pageInfo.getTotal(),pageInfo.getPageNum(),beanConverterUtil.toIncomeDTO(incomes));
    }

    @Override
    public IncomeDTO getIncomeById(Integer id) {
        return beanConverterUtil.toIncomeDTO(incomeMapper.selectByPrimaryKey(id));
    }

    @Override
    public void saveIncome(IncomeDTO incomeDTO) {
        Income income = beanConverterUtil.toIncome(incomeDTO);
        income.setCreateTime(new Date());
        incomeMapper.insert(income);
    }

    @Override
    public void updateIncome(IncomeDTO incomeDTO) {
        incomeMapper.updateByPrimaryKeySelective(beanConverterUtil.toIncome(incomeDTO));
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        incomeMapper.deleteByIds(ids);
    }
}
